package com.cloudpolice.equipment.controller;

import com.cloudpolice.equipment.model.EquipmentSiteModel;
import com.cloudpolice.equipment.service.EquipmentSiteService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lh.model.ResultVO;
import lh.units.ResultStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：梁昊，后端工程师：卢春龙，前端工程师：孙耀
 * @create 2020-04-21 14:04
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/equipmentSiteController")
@Api(value = "设备位置信息", description = "设备位置信息")
public class EquipmentSiteController {
    @Autowired
    EquipmentSiteService equipmentSiteService;

    /**
     * 根据sn号删除位置关系，方法ID：DE20200421140448408C2A6E92349A1
     *
     * @param sn 设备sn（外键）
     * @return 结果
     */
    @ApiOperation(value = "根据sn号删除位置关系,方法ID：DE20200421140448408C2A6E92349A1", notes = "结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "设备sn（外键）", dataType = "String", required = true)
    })
    @PostMapping("/deleteEquipmentSiteBySn")
    public ResultVO deleteEquipmentSiteBySn(@RequestParam(value = "sn") String sn) {
        sn = sn == null ? sn : sn.trim();

        int updateCount = equipmentSiteService.deleteEquipmentSiteBySn(sn);
        if (updateCount > 0)
            return ResultStruct.success(updateCount);
        else
            return ResultStruct.error("删除失败", ResultVO.class, int.class);
    }

    /**
     * 新增设备位置关系，方法ID：IN2020042114145510917524D51CC06
     *
     * @param sn 设备sn（外键）
     * @param roomType 0:场所，1：区域，2：楼栋，3:单元，4：房间，5:通道
     * @param siteId 位置ID(场所、社区区域、楼栋、单元、通道ID)
     * @return 结果
     */
    @ApiOperation(value = "新增设备位置关系,方法ID：IN2020042114145510917524D51CC06", notes = "结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "设备sn（外键）", dataType = "String", required = true)
            , @ApiImplicitParam(name = "roomType", value = "0:场所，1：区域，2：楼栋，3:单元，4：房间，5:通道", dataType = "String", required = true)
            , @ApiImplicitParam(name = "siteId", value = "位置ID(场所、社区区域、楼栋、单元、通道ID)", dataType = "String", required = true)
    })
    @PostMapping("/insertEquipmentSite")
    public ResultVO insertEquipmentSite(@RequestParam(value = "sn") String sn
            , @RequestParam(value = "roomType") int roomType
            , @RequestParam(value = "siteId") String siteId) {
        siteId = siteId == null ? siteId : siteId.trim();

        EquipmentSiteModel equipmentSiteModel = new EquipmentSiteModel();
        equipmentSiteModel.setSn(sn);
        equipmentSiteModel.setRoomType(roomType);
        equipmentSiteModel.setSiteId(siteId);
        int repetitionCount = equipmentSiteService.insertEquipmentSiteBeforeCheck(sn);
        if (repetitionCount > 0)
            return ResultStruct.error("增加失败，有" + repetitionCount + "条数据已重复！", ResultVO.class, int.class);
        int resultCount = equipmentSiteService.insertEquipmentSite(equipmentSiteModel);
        if (resultCount > 0)
            return ResultStruct.success(resultCount);
        else
            return ResultStruct.error("增加失败", ResultVO.class, int.class);
    }

    /**
     * 修改设备位置关系，方法ID：UP20200421143236166B27CA5DFBF65
     *
     * @param sn 设备sn（外键）
     * @param roomType 0:场所，1：区域，2：楼栋，3:单元，4：房间，5:通道
     * @param siteId 位置ID(场所、社区区域、楼栋、单元、通道ID)
     * @return 结果
     */
    @ApiOperation(value = "修改设备位置关系,方法ID：UP20200421143236166B27CA5DFBF65", notes = "结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "设备sn（外键）", dataType = "String", required = true)
            , @ApiImplicitParam(name = "roomType", value = "0:场所，1：区域，2：楼栋，3:单元，4：房间，5:通道", dataType = "String", required = true)
            , @ApiImplicitParam(name = "siteId", value = "位置ID(场所、社区区域、楼栋、单元、通道ID)", dataType = "String", required = true)
    })
    @PostMapping("/updateEquipmentSite")
    public ResultVO updateEquipmentSite(@RequestParam(value = "sn") String sn
            , @RequestParam(value = "roomType") int roomType
            , @RequestParam(value = "siteId") String siteId) {
        sn = sn == null ? sn : sn.trim();
        siteId = siteId == null ? siteId : siteId.trim();

        EquipmentSiteModel equipmentSiteModel = new EquipmentSiteModel();
        equipmentSiteModel.setSn(sn);
        equipmentSiteModel.setRoomType(roomType);
        equipmentSiteModel.setSiteId(siteId);
        int updateCount = equipmentSiteService.updateEquipmentSite(equipmentSiteModel);
        if (updateCount > 0)
            return ResultStruct.success(updateCount);
        else
            return ResultStruct.error("修改失败", ResultVO.class, int.class);
    }

    /**
     * 通用查询设备位置关系，方法ID：SE202004211455141849C2CBD5590C6
     *
     * @param sn 表vou_EquipmentSite,字段名sn:设备sn（外键）
     * @param roomType 表vou_EquipmentSite,字段名roomType:0:场所，1：区域，2：楼栋，3:单元，4：房间，5:通道
     * @param siteId 表vou_EquipmentSite,字段名siteId:位置ID(场所、社区区域、楼栋、单元、通道ID)
     * @param page 当前页数
     * @param limit 每页条数
     * @return 结果
     */
    @ApiOperation(value = "通用查询设备位置关系,方法ID：SE202004211455141849C2CBD5590C6", notes = "结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "设备sn（外键）", dataType = "String"),
            @ApiImplicitParam(name = "roomType", value = "0:场所，1：区域，2：楼栋，3:单元，4：房间，5:通道", dataType = "String"),
            @ApiImplicitParam(name = "siteId", value = "位置ID(场所、社区区域、楼栋、单元、通道ID)", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页数", dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "Integer")
    })
    @PostMapping("/getEquipmentSite")
    public PageInfo getEquipmentSite(@RequestParam(value = "sn", required = false) String sn
            , @RequestParam(value = "roomType", required = false) Integer roomType
            , @RequestParam(value = "siteId", required = false) String siteId
            , @RequestParam(value = "page", defaultValue = "1") int page
            , @RequestParam(value = "limit", defaultValue = "10") int limit) {
        sn = sn == null ? sn : sn.trim();
        siteId = siteId == null ? siteId : siteId.trim();

        EquipmentSiteModel equipmentSiteModel = new EquipmentSiteModel();
        equipmentSiteModel.setSn(sn);
        equipmentSiteModel.setRoomType(roomType);
        equipmentSiteModel.setSiteId(siteId);

        PageHelper.startPage(page, limit);
        List<EquipmentSiteModel> equipmentSiteModels = equipmentSiteService.getEquipmentSite(equipmentSiteModel);
        PageInfo pageInfo = new PageInfo<>(equipmentSiteModels, limit);
        return pageInfo;
    }




}

